function CollisionMap() {
	var collision_map =map.layers[2].data;
	var width_map = map.width;
	var height_map = map.height;
	var html = "";
	var SCREEN_WIDTH = 4096;
	var TILE_WIDTH = 64;
	var TILE_HEIGHT = 32;
	var SCREEN_HEIGHT = 2096;
	var grid2d = new Array(height_map);
	var x = 0;
	var y = 0;
	
	for ( var i = 0; i < width_map; i++) {
		grid2d[i] = new Array(height_map);
	}
	
	function map_to_screen(point) {
	   return {"x":(SCREEN_WIDTH + (point.y - point.x) * TILE_WIDTH) / 2,
	    "y":((SCREEN_HEIGHT + (point.y + point.x) * TILE_HEIGHT) / 2) - 1024}
	}
	
	var DOUBLED_TILE_AREA = 2 * TILE_HEIGHT * TILE_WIDTH
	var S2M_CONST_X = -SCREEN_HEIGHT * TILE_WIDTH + SCREEN_WIDTH * TILE_HEIGHT
	var S2M_CONST_Y = -SCREEN_HEIGHT * TILE_WIDTH - SCREEN_WIDTH * TILE_HEIGHT

	function screen_to_map(point) {
	    // the "+ TILE_HEIGHT/2" adjusts for the render offset
	    point.x = point.x * TILE_HEIGHT;
	    point.y = (point.y + TILE_HEIGHT/2) * TILE_WIDTH
	    x = (2 * (point.y - point.x) + S2M_CONST_X) / DOUBLED_TILE_AREA
	    y = (2 * (point.x + point.y) + S2M_CONST_Y) / DOUBLED_TILE_AREA
	    return {"x":Math.floor(x)+32, "y":Math.floor(y)+32}
	}
	
	this.createBlock = function(i,j,val,screen) {
		createBlock(i,j,val,screen);
	}
	
	function createBlock(i,j,val,screen) {
		if(screen) {
			var pos = {"x":i,"y":j};
		} else {
			var pos = map_to_screen({"x":i,"y":j});

		}
		var color;
		switch (val) {
		case 0:
			color = 'yellow';
			break;
		case 1:
			color = 'green'; 
			break;
		case 2:
			color = 'blue';
			break;
		case 3:
			color = 'red';
			break;
		default:
			break;
		}
		html="<div class='debugSq' style='position: absolute;opacity:1;width: 15px; height: 15px; border: 0px solid black; top: "+(pos.y-8)+"px; left: "+pos.x+"px; background-color:"+color+"'>"+val+"</div>";
		$("#canvas").append(html);
	}
	
	
	for ( var i = 0; i < collision_map.length; i++) {
		
		grid2d[ Math.floor(i/width_map)][i%64] = collision_map[i];
	}
	
//	for ( var i = 0; i < width_map; i++) {
//		for ( var j = 0; j < height_map; j++) {
//			//createBlock(i,j,grid2d[i][j])
//		}
//	}
	

	
	$("#canvas").on('mousemove',function(e) {
		var pos = {"x":e.pageX,"y":e.pageY}
		var mapcoords = screen_to_map(pos)
		var revscreen = map_to_screen(mapcoords)
		
		$("#debug").text(mapcoords.x+","+mapcoords.y+" : "+revscreen.x+","+revscreen.y+" : "+e.pageX+","+e.pageY);
	})
	
	//$("#canvas").append(html);
	
	function Point(y,x) {
		return {"x":x,"y":y};
	}
	
	function calcStraightLine (startCoordinates, endCoordinates) {
	    var coordinatesArray = new Array();
	    // Translate coordinates
	    var x1 = startCoordinates.x;
	    var y1 = startCoordinates.y;
	    var x2 = endCoordinates.x;
	    var y2 = endCoordinates.y;
	    // Define differences and error check
	    var dx = Math.abs(x2 - x1);
	    var dy = Math.abs(y2 - y1);
	    var sx = (x1 < x2) ? 1 : -1;
	    var sy = (y1 < y2) ? 1 : -1;
	    var err = dx - dy;
	    // Set first coordinates
	    coordinatesArray.push(new Point(y1, x1));
	    // Main loop
	    while (!((x1 == x2) && (y1 == y2))) {
	      var e2 = err << 1;
	      if (e2 > -dy) {
	        err -= dy;
	        x1 += sx;
	      }
	      if (e2 < dx) {
	        err += dx;
	        y1 += sy;
	      }
	      // Set coordinates
	      coordinatesArray.push(new Point(y1, x1));
	    }
	    // Return the result
	    return coordinatesArray;
	  }
	
	
	function checkLOS(s,e,filterFunc) {
		var result = calcStraightLine(s,e);
		var lastResult = s;
		for ( var i = 0; i < result.length; i++) {
			r = result[i];
			if(filterFunc(grid2d[r.x][r.y])) {
				return lastResult;
			} else {
				lastResult = r;
				continue;
			}
		}
		return lastResult;
	}
	
	function checkWalkability(s,e,filterFunc) {
		var result = calcStraightLine(s,e);
		//var lastResult = s;
		for ( var i = 0; i < result.length; i++) {
			r = result[i];
			if(filterFunc(grid2d[r.x][r.y])) {
				return false;
			} else {
				continue;
			}
		}
		return true;
	}
	
	this.checkLOS = function(s,e) {
		var sgrid = screen_to_map(s);
		var egrid = screen_to_map(e);
		return map_to_screen(checkLOS(sgrid,egrid,function(type) {
			return type != 0;
		}));
	}
	
	this.pathFind = function(s,e) {
		var sgrid = screen_to_map(s);
		var egrid = screen_to_map(e);
		//createBlock(sgrid.x,sgrid.y,grid2d[sgrid.x][sgrid.y]);
//		createBlock(egrid.x,egrid.y,grid2d[egrid.x][egrid.y]);
		
		setWall(function() {
		    return this.type != 0;
		})
		filterFunc = function(type) {
			return type != 0;
		}
		var graph = new Graph(grid2d);
		var start = graph.nodes[sgrid.x][sgrid.y];
		var end = graph.nodes[egrid.x][egrid.y];
		var result = astar.search(graph.nodes, start, end);
		var screen_result = []
		
		for ( var i = 0; i < result.length; i++) {
			result[i].order = i;
		}
		
		var trimmed_result = result.slice(0);
		
		function canWalk(one,two) {
			return checkWalkability(one,two,filterFunc)
		}
		
		
		var r = 0;
	
		while (r < trimmed_result.length-1) {
			var standing = trimmed_result[r];
			if(canWalk(standing,trimmed_result[r+1])) {
				trimmed_result.splice(r+1,1);
			} else {
				r = r+1;
			}
		}
		
//		console.log("org",result)
//		console.log("trimmed",trimmed_result)
		trimmed_result.push(egrid)
		trimmed_result.sort(function(x,y) {return x.order > y.order })
		
		//trimmed_result.reverse();
		for ( var i = 0; i < trimmed_result.length; i++) {
			r = trimmed_result[i]
			collisionMap.createBlock(r.x,r.y,i);
			screen_result.push(map_to_screen(r))
			
		}
		
		
		return screen_result;
	}
}

var wallFunc;
function setWall(f) {
	wallFunc = f;
	GraphNode.prototype.isWall = f;
}
 
